package com.workpaper.app.workpaper.daily.view;

import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.part.ViewPart;

import com.workpaper.app.cache.resource.ViewInfoCache;
import com.workpaper.app.consts.common.CommonViewConsts;
import com.workpaper.app.consts.workpaper.DailyWorkpaperViewConsts;
import com.workpaper.app.workpaper.daily.actions.AddDailyWorkpaperAction;
import com.workpaper.app.workpaper.daily.actions.RefreshDailyWorkpaperAction;
import com.workpaper.app.workpaper.daily.business.DailyWorkpaperBusiness;
import com.workpaper.app.workpaper.daily.providers.DailyWorkpaperTableContentProvider;
import com.workpaper.app.workpaper.daily.providers.DailyWorkpaperTableLabelProvider;
import com.workpaper.app.workpaper.daily.view.utils.DailyWorkpaperViewUtils;

public class DailyWorkpaperManagerView extends ViewPart {
	private Table table;
	private TableViewer tableViewer;
	public DailyWorkpaperManagerView() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		initTableViewer(parent);
		createColumns();
        
		//注册标签提供器
        tableViewer.setLabelProvider(new DailyWorkpaperTableLabelProvider());
        //注册内容提供器
        tableViewer.setContentProvider(new DailyWorkpaperTableContentProvider());
        //输入数据
        refreshTableData();        
        //选中第一条、并获取焦点： 非必须
//        table.setSelection(0);
//        table.setFocus();		

        //将TableViewer注册成Select事件源，因为后续需要处理选中事件
//        getViewSite().setSelectionProvider(tableViewer);
        
        //增加工具栏
        createToolBar();
	}

	private void createToolBar() {
		IToolBarManager toolBar = getViewSite().getActionBars().getToolBarManager();
        toolBar.add(new RefreshDailyWorkpaperAction(this));
        toolBar.add(new AddDailyWorkpaperAction());
	}

	/**
	 * 刷新数据
	 */
	public void refreshTableData() {
        //输入数据
		 ((DailyWorkpaperTableLabelProvider)tableViewer.getLabelProvider()).reset();
        tableViewer.setInput(getAllData());
        DailyWorkpaperViewUtils.getInstance().addRowOperatingButtons(tableViewer);
	}
	
	
	
	private void createColumns() {
		//序号列
		TableViewerColumn orderColTV = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn orderCol = orderColTV.getColumn();
		orderCol.setWidth(50);
		orderCol.setText(ViewInfoCache.get(CommonViewConsts.TableHead_Order));
		orderCol.setResizable(false);	//不能调节宽度
		orderCol.setMoveable(false); 	//不能拖拽移动列的位置
				
		//日期列
		TableViewerColumn dateColTV = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn dateCol = dateColTV.getColumn();
		dateCol.setWidth(100);
		dateCol.setText(ViewInfoCache.get(DailyWorkpaperViewConsts.Wpd_ManageTableHead_Date));
		
		//内容列
		TableViewerColumn contentColTV = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn contentCol = contentColTV.getColumn();
		contentCol.setWidth(500);
		contentCol.setText(ViewInfoCache.get(DailyWorkpaperViewConsts.Wpd_ManageTableHead_Content));
		
		//所有人列
		TableViewerColumn masterColTV = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn  masterCol = masterColTV.getColumn();
		masterCol.setWidth(100);
		masterCol.setText(ViewInfoCache.get(DailyWorkpaperViewConsts.Wpd_ManageTableHead_Master));
		
		//备注列
		TableViewerColumn remarkColTV = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn remarkCol = remarkColTV.getColumn();
		remarkCol.setWidth(200);
		remarkCol.setText(ViewInfoCache.get(DailyWorkpaperViewConsts.Wpd_ManageTableHead_Remark));
		//id
		TableViewerColumn idColTV = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn idCol = idColTV.getColumn();
		idCol.setWidth(0);
		idCol.setResizable(false);
		
		//操作列
		TableViewerColumn operateColTV = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn operateCol = operateColTV.getColumn();
		operateCol.setWidth(127);
		operateCol.setResizable(false);
		operateCol.setText(ViewInfoCache.get(CommonViewConsts.TableHead_Operation));
	}


	@Override
	public void setFocus() {
	}
	
	private void initTableViewer(Composite parent) {
		parent.setLayout(new FillLayout(SWT.HORIZONTAL));	//父容器布局： 水平布局
		tableViewer = new TableViewer(parent, SWT.BORDER | SWT.FULL_SELECTION);	//创建tableViewer
		tableViewer.setUseHashlookup(true);	//true：提高SWT元素与数据的映射效率
		initTable();
	}
	
	private void initTable() {
		table = tableViewer.getTable();	//获取Table
		table.setLayout(new TableLayout());	//设置table的布局，TableLayout专门用于表格布局
		
		table.setBackgroundImage(null);	//表格背景图片：null
		//table.setLayoutData(new GridData(GridData.FILL_BOTH));
		table.setHeaderVisible(true);	//显示表头
		table.setLinesVisible(true);	//显示边框线
	}
	
	private Object getAllData() {
		return DailyWorkpaperBusiness.getInstance().getAllWorkpaperDaily();
	}
	
}
